package com.geeker.module.sys.dict.sevice;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.geeker.common.exception.BusinessException;
import com.geeker.common.util.HuExcelUtil;
import com.geeker.module.common.util.DictUtils;
import com.geeker.module.sys.dict.domain.SysDictData;
import com.geeker.module.sys.dict.domain.SysDictType;
import com.geeker.module.sys.dict.dto.SysDictTypeDto;
import com.geeker.module.sys.dict.mapper.SysDictTypeMapper;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class SysDictTypeService extends ServiceImpl<SysDictTypeMapper, SysDictType> {

    @Autowired
    private SysDictDataService sysDictDataService;

    public boolean add(SysDictType dict) {
        List<SysDictType> sysDictTypes = this.list(QueryWrapper.create().eq(SysDictType::getDictType, dict.getDictType()));
        if (CollectionUtil.isNotEmpty(sysDictTypes))
            throw new BusinessException("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        boolean insert = this.save(dict);
        if (insert)
            DictUtils.setDictCache(dict.getDictType(), null);
        return insert;
    }

    public boolean edit(SysDictType dict) {
        List<SysDictType> sysDictTypes = this.list(QueryWrapper.create().eq(SysDictType::getDictType, dict.getDictType()));
        if (CollectionUtil.isNotEmpty(sysDictTypes) && !Objects.equals(sysDictTypes.getFirst().getDictId(), dict.getDictId()))
            throw new BusinessException("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        boolean bool = this.updateById(dict);
        if (bool) {
            List<SysDictData> dictDatas = sysDictDataService.getByDictType(dict.getDictType());
            DictUtils.setDictCache(dict.getDictType(), dictDatas);
        }
        return bool;
    }

    public void delete(List<Long> dictIds) {
        for (Long dictId : dictIds) {
            SysDictType dictType = this.getById(dictId);
            if (dictType == null)
                throw new BusinessException("参数错误");
            long count = sysDictDataService.count(QueryWrapper.create().eq(SysDictType::getDictType, dictType.getDictType()));
            if (count > 0) {
                throw new BusinessException(StrUtil.format("{}已分配,不能删除", dictType.getDictName()));
            }
            this.removeById(dictId);
            DictUtils.removeDictCache(dictType.getDictType());
        }
    }

    public void resetDictCache() {
        DictUtils.clearDictCache();
        Map<String, List<SysDictData>> dictDataMap = sysDictDataService.list(QueryWrapper.create().eq(SysDictData::getStatus, 1)).stream().collect(Collectors.groupingBy(SysDictData::getDictType));
        for (Map.Entry<String, List<SysDictData>> entry : dictDataMap.entrySet()) {
            DictUtils.setDictCache(entry.getKey(), entry.getValue().stream().sorted(Comparator.comparing(SysDictData::getDictSort)).collect(Collectors.toList()));
        }
    }

    public void export(SysDictTypeDto dto) {
        List<SysDictType> list = this.list();
        HuExcelUtil.exportExcel("字典类型", SysDictType.class, list);
    }
}
